package com.cplatform.movie.back.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cplatform.movie.back.service.BsFileService;
import com.cplatform.util2.TimeStamp;

/**
 * 标题、简要说明.
 * <p>
 * 类详细说明.
 * <p>
 * Copyright: Copyright (c) 2013-6-6 下午3:15:22
 * <p>
 * Company: 北京宽连十方数字技术有限公司
 * <p>
 * 
 * @author zhengmz@c-platform.com
 * @version 1.0.0
 */

@Component
public class PathUtil {

	@Autowired
	AppConfig appConfig;

	/**
	 * 获得模块前缀
	 * 
	 * @param module
	 *            模块key
	 * @return
	 */
	public String getKeyPrefix(String key) {
		if (BsFileService.ITEM_SALE_COVER_KEY.equals(key)) {
			return appConfig.getUploadItemCoverDir();
		}
		if (BsFileService.ITEM_SALE_INFO_KEY.equals(key)) {
			return appConfig.getUploadItemInfoDir();
		}
		if (BsFileService.ITEM_SALE_TYPE_KEY.equals(key)) {
			return appConfig.getUploadItemTypeDir();
		}
		if (BsFileService.STORE_INFO_KEY.equals(key)) {
			return appConfig.getUploadStoreInfoDir();
		}
		if (BsFileService.ACTIVITIES_INFO_KEY.equals(key)) {
			return appConfig.getUploadActivitiesInfoDir();
		}
		return "";
	}

	/**
	 * 获取不同业务类型的文件生成路径
	 * 
	 * @param key
	 *            业务key
	 * @param id
	 *            业务id
	 * @param ext
	 *            扩展名
	 * @return
	 */
	public PathInfo getPathById(String key, long id, String ext) {

		String webpath = "";
		String savepath = "";// 保存路径
		String filename = "";// 文件名称
		ext = ext.toLowerCase();

		/*
		 * 算法： 第一层 = ID/(20002000) 第二层= (ID mod （2000×2000））/2000 第三层 = ID mod
		 * 2000
		 */
		long l1 = id / (2000 * 2000);

		long l2 = (id % (2000 * 2000)) / 2000;

		long l3 = id % 2000;

		/** 商品封面图片 **/
		if (BsFileService.ITEM_SALE_COVER_KEY.equals(key)) {
			webpath = String.format("%d/%d/%d/N/", l1, l2, l3);
			savepath = String.format("%d/%d/%d/N/", l1, l2, l3);
			filename = String.format("p_%d%d%d.%s", l1, l2, l3, ext);
			return new PathInfo(appConfig.getUploadItemCoverDir() + savepath, webpath, filename);
		}
		
		/** 商品详情图片 **/
		if (BsFileService.ITEM_SALE_INFO_KEY.equals(key)) {
			webpath = String.format("%d/%d/%d/N/", l1, l2, l3);
			savepath = String.format("%d/%d/%d/N/", l1, l2, l3);
			filename = String.format("p_%d%d%d.%s", l1, l2, l3, ext);
			return new PathInfo(appConfig.getUploadItemInfoDir() + savepath, webpath, filename);
		}
		
		/** 商品分类图片 **/
		if (BsFileService.ITEM_SALE_TYPE_KEY.equals(key)) {
			webpath = String.format("%d/%d/%d/N/", l1, l2, l3);
			savepath = String.format("%d/%d/%d/N/", l1, l2, l3);
			filename = String.format("p_%d%d%d.%s", l1, l2, l3, ext);
			return new PathInfo(appConfig.getUploadItemTypeDir() + savepath, webpath, filename);
		}
		
		/** 商户图片 **/
		if (BsFileService.STORE_INFO_KEY.equals(key)) {
			webpath = String.format("%d/%d/%d/N/", l1, l2, l3);
			savepath = String.format("%d/%d/%d/N/", l1, l2, l3);
			filename = String.format("p_%d%d%d.%s", l1, l2, l3, ext);
			return new PathInfo(appConfig.getUploadStoreInfoDir() + savepath, webpath, filename);
		}
		
		/** 活动图片 **/
		if (BsFileService.ACTIVITIES_INFO_KEY.equals(key)) {
			webpath = String.format("%d/%d/%d/N/", l1, l2, l3);
			savepath = String.format("%d/%d/%d/N/", l1, l2, l3);
			filename = String.format("p_%d%d%d.%s", l1, l2, l3, ext);
			return new PathInfo(appConfig.getUploadActivitiesInfoDir() + savepath, webpath, filename);
		}
		
		// 其他情况
		webpath = String.format("other/%d/%d/%d/", l1, l2, l3);
		savepath = String.format("other/%d/%d/%d/", l1, l2, l3);
		filename = String.format("o_%d%d%d_%s.%s", l1, l2, l3, TimeStamp.getTime(TimeStamp.YYYYMMDDhhmmssxxx), ext);
		return new PathInfo(appConfig.getUploadTmpDir() + savepath, webpath, filename);
	}

	/**
	 * 获取商户移动签约签署协议附件生成路径
	 * 
	 * @author liujun 2013-09-
	 * @param key
	 *            业务key
	 * @param id
	 *            业务id
	 * @param ext
	 *            扩展名
	 * @return
	 */
	public PathInfo getBusiContractPathById(String key, long id, String ext) {

		String webpath = "";
		String savepath = "";// 保存路径
		String filename = "";// 文件名称
		ext = ext.toLowerCase();

		/*
		 * 算法： 第一层 = ID/(20002000) 第二层= (ID mod （2000×2000））/2000 第三层 = ID mod
		 * 2000
		 */
		long l1 = id / (2000 * 2000);

		long l2 = (id % (2000 * 2000)) / 2000;

		long l3 = id % 2000;

		// 其他情况
		webpath = String.format("other/%d/%d/%d/", l1, l2, l3);
		savepath = String.format("other/%d/%d/%d/", l1, l2, l3);
		filename = String.format("o_%d%d%d_%s.%s", l1, l2, l3, TimeStamp.getTime(TimeStamp.YYYYMMDDhhmmssxxx), ext);
		return new PathInfo(appConfig.getUploadTmpDir() + savepath, webpath, filename);
	}

	public static class PathInfo {

		public PathInfo(String savepath, String webpath, String filename) {
			this.savepath = savepath;
			this.webPath = webpath;
			this.filename = filename;
		}

		private String savepath;

		private String filename;

		private String webPath;

		public String getRealPath() {
			return this.getSavepath() + this.getFilename();
		}

		/**
		 * 获得真实的web访问领
		 * 
		 * @param contextPath
		 *            是否有前置
		 * @param ext
		 *            扩展名
		 * @return
		 */
		public String getRealWebPath(String contextPath) {
			return contextPath + this.getWebPath() + this.getFilename();
		}

		public String getFilename() {
			return filename;
		}

		public void setFilename(String filename) {
			this.filename = filename;
		}

		public String getWebPath() {
			return webPath;
		}

		public void setWebPath(String webPath) {
			this.webPath = webPath;
		}

		public String getSavepath() {
			return savepath;
		}

		public void setSavepath(String savepath) {
			this.savepath = savepath;
		}

	}

	// public static String getExtName(String fileName){
	//
	// String extName="";
	// //int indexPos=0;
	//
	// if(!"".equals(fileName)&&fileName!=null){
	// if(fileName.indexOf(".")>-1){
	//
	// extName=fileName.substring(fileName.indexOf(".")+1);
	// }
	//
	// }
	// return extName;
	//
	// }

	public static void main(String[] args) {
		// /long number = 7999999999L;

		// /String fileName="test.doc";

		// /System.out.println(PathUtil.getExtName(fileName));

		// long maxFeeAmount=Long.valueOf("49999.99");
		double a1 = Double.parseDouble("4999.99") * 100;
		System.out.println(new Double(a1).longValue() + "");

	}

	public PathInfo getPathInfo(String module, String ext, Long... ids) {
		String webpath = "";
		String savepath = "";// 保存路径
		String filename = "";// 文件名称
		ext = ext.toLowerCase();
		String time = TimeStamp.getTime(TimeStamp.YYYYMMDDhhmmssxxx);
		String dir1 = time.substring(0, 8);
		String dir2 = time.substring(8, 10);
		String dir3 = time.substring(10);

		return null;
	}

	/**
	 * 获取商品图片规则，获得第几个图片
	 * 
	 * @param id
	 * @param indx
	 * @return
	 */
	public static String getItemPic(Long id, int indx) {
		long l1 = id / (2000 * 2000);
		long l2 = (id % (2000 * 2000)) / 2000;
		long l3 = id % 2000;
		String savepath = String.format("%d/%d/%d/N%d/", l1, l2, l3, indx);
		return savepath;
	}

}
